package com.example.count.controller;

import com.example.count.mapper.InfocountMapper;
import com.example.count.mapper.OrderMapper;
import com.example.count.mapper.VisitorMapper;
import com.example.count.pojo.Infocount;
import com.example.count.pojo.Order;
import com.example.count.pojo.Visitor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName CountController
 * @Description TODO
 * @Author xiguan
 * @Date 2021/12/28 15:13
 * @Version 1.0.0
 **/
@RestController
@RequestMapping("/count")
public class CountController {

    @Autowired
    private InfocountMapper infocountMapper;
    @Autowired
    private VisitorMapper visitorMapper;
    @Autowired
    private OrderMapper orderMapper;


    @RequestMapping("/getAll")
    public List<Infocount> findAll(){
        return infocountMapper.selectAll();
    }
    /**
     * 导出统计访客信息excel
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping("/static")
    public ArrayList statistics(@RequestParam(value = "startTime",required = false) Date startTime, @RequestParam(value = "endTime",required = false) Date endTime) {
        Example enter = new Example(Visitor.class);
        Example leave = new Example(Visitor.class);
        Example ordercount = new Example(Order.class);
        Example orderPass = new Example(Order.class);
        Example orderdeny = new Example(Order.class);
        Example.Criteria criteria = enter.createCriteria();
        Example.Criteria criterialeave = leave.createCriteria();
        Example.Criteria criteriaorder = ordercount.createCriteria();
        Example.Criteria criteriaorderget = orderPass.createCriteria();
        Example.Criteria criteriaorderrefuse = orderdeny.createCriteria();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (startTime == null && endTime == null) {
            try {
                startTime = dateFormat.parse("2020-01-00 00:00:00");
                endTime = new Date();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else if (endTime == null) {
            endTime = new Date();
        } else if (startTime == null) {
            try {
                startTime = dateFormat.parse("2020-01-00 00:00:00");
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //时间段进入人数
        criteria.andBetween("etime", startTime, endTime);
        Integer enterNumber = visitorMapper.selectCountByExample(enter);
        //时间段离开人数
        criterialeave.andBetween("ltime", startTime, endTime);
        Integer leaveNumber = visitorMapper.selectCountByExample(leave);
        //总预约人数
        criteriaorder.andBetween("orderVisittime", startTime, endTime);
        Integer orderCount = orderMapper.selectCountByExample(ordercount);
        //预约通过人数
        criteriaorderget.andEqualTo("orderStatus", 1);
        criteriaorderget.andBetween("orderVisittime", startTime, endTime);
        Integer orderpass = orderMapper.selectCountByExample(orderPass);
        //预约未通过人数
        criteriaorderrefuse.andEqualTo("orderStatus", 0);
        criteriaorderrefuse.andBetween("orderVisittime", startTime, endTime);
        Integer orderrefuse = orderMapper.selectCountByExample(orderdeny);
        ArrayList arrayList = new ArrayList();
        //使用@ResponseBody，返回的map会被springmvc使用json lib 转换成json字符串
        //组合成 json字符串"{\"name\" : \"zhangsan\", \"age\" : 20}"
        Map map = new HashMap();
        map.put("name", "进入人数");
        map.put("value", enterNumber);
        arrayList.add(map);
        Map map1 = new HashMap();
        map1.put("name", "离开人数");
        map1.put("value", leaveNumber);
        arrayList.add(map1);
        Map map2 = new HashMap();
        map2.put("name", "预约总人数");
        map2.put("value", orderCount);
        arrayList.add(map2);
        Map map3 = new HashMap();
        map3.put("name", "预约通过人数");
        map3.put("value", orderpass);
        arrayList.add(map3);
        Map map4 = new HashMap();
        map4.put("name", "预约拒绝人数");
        map4.put("value", orderrefuse);
        arrayList.add(map4);
        //将统计出来的数据添加到数据库
        Infocount infocount = new Infocount();
        infocount.setInternumber(enterNumber);
        infocount.setLeavenumber(leaveNumber);
        infocount.setOrdercount(orderCount);
        infocount.setOrderpassnumber(orderpass);
        infocount.setOrderrejectnumber(orderrefuse);
        infocount.setGetday(new Date());
        infocountMapper.insertSelective(infocount);
        return arrayList;
    }

}
